﻿using System.Collections.Generic;
using System.Text;

namespace SocialKit.LightRest
{
    /// <summary>
    /// Represents an empty content.
    /// </summary>
    public class EmptyContent : IHttpContentCreator
    {
        readonly byte[] buffer;

        /// <summary>
        /// Create an instance of SocialKit.LightRest.EmptyContent class
        /// </summary>
        public EmptyContent()
        {
            this.buffer = new byte[0] { };
        }

        #region IHttpContentCreator Members

        /// <summary>
        /// Gets the encoding of current content, always returns null of an empty content.
        /// </summary>
        public Encoding ContentEncoding
        {
            get { return null; }
            set { }
        }

        /// <summary>
        /// Gets the content type of current content, always returns null of an empty content.
        /// </summary>
        public string ContentType
        {
            get { return null; }
        }

        /// <summary>
        /// Returns an empty byte array.
        /// </summary>
        /// <returns></returns>
        public byte[] CreateContent()
        {
            return this.buffer;
        }

        #endregion

        /// <summary>
        /// Parse a string to a url encoded form or an empty content.
        /// </summary>
        /// <param name="values">The URI query string to parse.</param>
        /// <returns></returns>
        public static IHttpContentCreator Parse(string values)
        {
            if (string.IsNullOrEmpty(values))
                return new EmptyContent();

            return new HttpUrlEncodedForm(RestUtility.ParseQueryString(values));
        }
    }
}